package com.zdkj.subway.check.controller.subwayRat.TVMRevenueVarianceAudit.TvmChaYiDXC;

import com.alibaba.fastjson.JSONObject;
import com.zdkj.subway.check.controller.CommonController;
import com.zdkj.subway.check.entity.Page;
import com.zdkj.subway.check.entity.SwAuTvmCashboxAmountHierarchyDifference;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwAuTvmCashboxAmountHierarchyDifferenceMapper;
import com.zdkj.subway.check.result.Result;
import com.zdkj.subway.check.service.SwAuTvmCashboxAmountHierarchyDifferenceService;
import com.zdkj.subway.check.util.UserInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据协查管理-TVM票款差异协查-TVM系统差异协查-TVM层级差异-分钱箱层级差异
 * TVM票款差异协查-现金层级差异-分钱箱信息 SW_AU_TVM_CASHBOX_AMOUNT_HIERARCHY_DIFFERENCE
 */
@Slf4j
@RestController
@RequestMapping("/checkNew")
public class SwAuTvmCashboxAmountHierarchyDifferenceCtrl {

    @Resource
    private SwAuTvmCashboxAmountHierarchyDifferenceService swAuTvmCashboxAmountHierarchyDifferenceService;
    @Resource
    private SwAuTvmCashboxAmountHierarchyDifferenceMapper swAuTvmCashboxAmountHierarchyDifferenceMapper;
    @Autowired
    private CommonController commonController;
    /**
     * TVM票款差异协查-现金层级差异-分钱箱信息 列表
     * @param jsonObject
     * @return
     */
    @RequestMapping("/getSwAuTvmCashboxAmountHierarchyDifferenceList")
    @ApiOperation(value = "TVM票款差异协查-现金层级差异-分钱箱信息 列表", notes = "TVM票款差异协查-现金层级差异-分钱箱信息 列表")
    public Page getSwAuTvmCashboxAmountHierarchyDifferenceList(Page page, @RequestBody JSONObject jsonObject){
        Map req = JSONObject.parseObject(jsonObject.toJSONString(), HashMap.class);
        req.put("assistStatusList","2,3");
        List<Map<String, Object>> swAuTvmCashboxAmountHierarchyDifferenceList = swAuTvmCashboxAmountHierarchyDifferenceMapper.getSwAuTvmCashboxAmountHierarchyDifferenceList(req);
        page.setData(swAuTvmCashboxAmountHierarchyDifferenceList);
        return page;
    }

    /**
     * TVM票款差异协查-现金层级差异-分钱箱信息 单独查询
     * @param jsonObject
     * @return
     */
    @RequestMapping("/getSwAuTvmCashboxAmountHierarchyDifference")
    @ApiOperation(value = "TVM票款差异协查-现金层级差异-分钱箱信息 单独查询", notes = "TVM票款差异协查-现金层级差异-分钱箱信息 单独查询")
    public Result getSwAuTvmCashboxAmountHierarchyDifference(@RequestBody JSONObject jsonObject){
        return swAuTvmCashboxAmountHierarchyDifferenceService.getSwAuTvmCashboxAmountHierarchyDifference(jsonObject);
    }

    /**
     * TVM票款差异协查-现金层级差异-分钱箱信息 修改
     * @param jsonObject
     * @return
     */
    @RequestMapping("/updateSwAuTvmCashboxAmountHierarchyDifference")
    @ApiOperation(value = "TVM票款差异协查-现金层级差异-分钱箱信息 修改", notes = "TVM票款差异协查-现金层级差异-分钱箱信息 修改")
    @Transactional(rollbackFor = Exception.class)
    public Result updateSwAuTvmCashboxAmountHierarchyDifference(@RequestBody JSONObject jsonObject){
        Result res=new Result(ResultDetailMsg.OK);
        try{
            String ids = jsonObject.getString("ids");
            for(String id:ids.split(",")){
                SwAuTvmCashboxAmountHierarchyDifference swAuTvmCashboxAmountHierarchyDifference = JSONObject.parseObject(jsonObject.toJSONString(), SwAuTvmCashboxAmountHierarchyDifference.class);
                swAuTvmCashboxAmountHierarchyDifference.setId(Integer.parseInt(id));
                swAuTvmCashboxAmountHierarchyDifference.setJdOperatorCode(UserInfo.getUserCode());
                swAuTvmCashboxAmountHierarchyDifference.setJdOperatorName(UserInfo.getName());
                int i = swAuTvmCashboxAmountHierarchyDifferenceMapper.updateById(swAuTvmCashboxAmountHierarchyDifference);
            }
            Map param = JSONObject.parseObject(jsonObject.toString(),Map.class);
            param.put("type",jsonObject.getString("innerBatchType"));
            commonController.innerBatch(param);
        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            res.setMsg("回馈失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return res;
    }

    /**
     * TVM票款差异协查-现金层级差异-分钱箱信息 回馈原因提交
     * @param swAuTvmCashboxAmountHierarchyDifference
     * @return
     */
    @RequestMapping("/submitSwAuTvmCashboxAmountHierarchyDifference")
    @ApiOperation(value = "TVM票款差异协查-现金层级差异-分钱箱信息 回馈原因提交", notes = "TVM票款差异协查-现金层级差异-分钱箱信息 回馈原因提交")
    public Result submitSwAuTvmCashboxAmountHierarchyDifference(SwAuTvmCashboxAmountHierarchyDifference swAuTvmCashboxAmountHierarchyDifference){
        Result res=new Result(ResultDetailMsg.OK);
        try{
            SwAuTvmCashboxAmountHierarchyDifference u=new SwAuTvmCashboxAmountHierarchyDifference();
            u.setId(swAuTvmCashboxAmountHierarchyDifference.getId());
            u.setReplySystemAmount(swAuTvmCashboxAmountHierarchyDifference.getReplySystemAmount());
            u.setJdReplyReason(swAuTvmCashboxAmountHierarchyDifference.getJdReplyReason());
            u.setJdReplyDetail(swAuTvmCashboxAmountHierarchyDifference.getJdReplyDetail());
            u.setJdReplyTime(new Date());
            swAuTvmCashboxAmountHierarchyDifferenceMapper.updateById(u);
        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            res.setMsg("回馈失败");
        }
        return res;
    }

    /**
     * TVM票款差异协查-现金层级差异-分钱箱信息 批量回馈原因
     * @param swAuTvmCashboxAmountHierarchyDifference
     * @return
     */
    @RequestMapping("/batchSwAuTvmCashboxAmountHierarchyDifference")
    @ApiOperation(value = "TVM票款差异协查-现金层级差异-分钱箱信息 批量回馈原因", notes = "TVM票款差异协查-现金层级差异-分钱箱信息 批量回馈原因")
    public Result batchSwAuTvmCashboxAmountHierarchyDifference(String ids,SwAuTvmCashboxAmountHierarchyDifference swAuTvmCashboxAmountHierarchyDifference){
        Result res=new Result(ResultDetailMsg.OK);
        try{
            for(String id:ids.split(",")){
                SwAuTvmCashboxAmountHierarchyDifference u=new SwAuTvmCashboxAmountHierarchyDifference();
                u.setId(Integer.parseInt(id));
                //回馈原因
                u.setJdReplyReason(swAuTvmCashboxAmountHierarchyDifference.getJdReplyReason());
                //回馈描述
                u.setJdReplyDetail(swAuTvmCashboxAmountHierarchyDifference.getJdReplyDetail());
                u.setJdReplyTime(new Date());
                swAuTvmCashboxAmountHierarchyDifferenceMapper.updateById(u);
            }
        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            res.setMsg("批量回馈失败");
        }
        return res;
    }

}
